package com.cysion.study.ui.fragment

import android.text.TextUtils
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.cysion.study.ktbox.Box
import com.cysion.study.ktbox.base.BaseFragment
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.utils.ACache
import com.cysion.study.ktbox.utils.CLog
import com.cysion.other.addTo
import com.cysion.other.startActivity_ex
import com.cysion.targetfun._subscribe
import com.cysion.study.R
import com.cysion.study.helper.UserCache
import com.cysion.study.net.ApiClient
import com.cysion.study.ui.MoneyActivity
import com.cysion.study.ui.PrivateManageActivity
import com.cysion.study.ui.TutorOrderActivity
import com.cysion.study.ui.login.LoginActivity
import com.cysion.study.ui.personal.*
import io.reactivex.disposables.CompositeDisposable
import kotlinx.android.synthetic.main.fragment_me_private.*

/**
 * 未使用
 * Created by chu on 2019-06-30.
 */
class PrivateMeFragment : BaseFragment(), View.OnClickListener {

    val compositeDisposable: CompositeDisposable = CompositeDisposable()
    override fun getLayoutId(): Int = R.layout.fragment_me_private

    override fun initView() {
        cv_head.setOnClickListener(this)
        tv_userName.setOnClickListener(this)
        tv_teacher.setOnClickListener(this)
        tv_attpe.setOnClickListener(this)
        tv_money.setOnClickListener(this)
        tv_setting.setOnClickListener(this)
        tv_yp.setOnClickListener(this)
        tv_order.setOnClickListener(this)
        tv_site.setOnClickListener(this)
    }

    override fun onResume() {
        super.onResume()
        CLog.i("onResume")
        updateUserInfo()
    }

    override fun onClick(v: View?) {
        if (TextUtils.isEmpty(UserCache.userId)) {
            context.startActivity_ex<LoginActivity>()
            return
        }
        when (v?.id) {
            R.id.cv_head -> {//个人信息
                context.startActivity_ex<PersonalInfoActivity>()
            }
            R.id.tv_userName -> {
                context.startActivity_ex<PersonalInfoActivity>()
            }
            R.id.tv_teacher -> {
//                ShowToast("老师")
                UserCache.clear()
                context.startActivity_ex<LoginActivity>()
                context.finish()
            }
            R.id.tv_attpe -> {//家教信息管理
                context.startActivity_ex<PrivateManageActivity>()
            }
            R.id.tv_money -> {//余额
                context.startActivity_ex<MoneyActivity>()
            }
            R.id.tv_yp -> {//应聘
                context.startActivity_ex<MyAcceptReciutActivity>()
            }
            R.id.tv_order -> {//家教订单
                context.startActivity_ex<TutorOrderActivity>()
            }
            R.id.tv_setting -> {//设置
                context.startActivity_ex<SettingActivity>()
            }
            R.id.tv_site -> {//场地订单
                context.startActivity_ex<MyTenancyListActivity>()

            }

        }

    }


    override fun lazyLoad() {
        super.lazyLoad()
        if (TextUtils.isEmpty(UserCache.token)) {
            CLog.i("lazyLoad")
            context.startActivity_ex<LoginActivity>()
            return
        }

        getUserInfos()

    }

    private fun getUserInfos() {
        ApiClient.api.getUserInfos(UserCache.token).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    if (!TextUtils.isEmpty(it.id)) {
                        UserCache.save(it)
                        updateUserInfo()
                    }
                }
            }.addTo(compositeDisposable)
    }

    private fun updateUserInfo() {

        if (TextUtils.isEmpty(UserCache.userInfo?.nickName ?: "")) {
            getUserInfos()
            return
        }
        Glide.with(context).load(UserCache.userInfo?.headImageUrl).apply(
            RequestOptions().placeholder(R.mipmap.default_head)
        ).into(cv_head)
        tv_userName.text = UserCache.userInfo?.nickName ?: "未登录"
        tv_money.text = "我的余额 ￥ " + UserCache.userInfo?.money

        var ttype= ACache.get(Box.context).getAsString("teacherType")
        if (ttype == "1") {//普通教师首页fragment
            tv_teacher.text = "普通教师"
        } else {
            tv_teacher.text = "私教"
        }

    }

    override fun visibleAgain() {
        super.visibleAgain()
        CLog.i("visibleAgain中的token=" + UserCache.token)
        if (TextUtils.isEmpty(UserCache.token)) {
            CLog.i("visibleAgain")
            context.startActivity_ex<LoginActivity>()
            return
        }
        updateUserInfo()
    }

    override fun closeMvp() {
        compositeDisposable.dispose()
    }
}